package com.agsmobi.digitapp;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class EnergyBarActivity extends BaseActivity implements OnClickListener {

	int duration;
	int orientation;
	int positionX;
	int positionY;
	int width;
	int height;
	LinearLayout liLayoutMaxProgress;
	TextView textProgress;
	Button btnStart;
	Button btnStop;
	Handler handler ;
	int timeUpdate;
	int lengthStep;
	int currentLong;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_energybar);
		loadControl();
		setDemoData();
		show();
	}
	
	@Override
	protected void loadControl() {
		// TODO Auto-generated method stub
		super.loadControl();
		liLayoutMaxProgress = (LinearLayout)findViewById(R.id.liLayoutMaxProgress);
		textProgress = (TextView)findViewById(R.id.progress);
		btnStart = (Button)findViewById(R.id.btnStart);
		btnStop = (Button)findViewById(R.id.btnStop);
		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);
	}
	
	public void setDemoData () {
		setDuration(10000);
		setOrientation(BaseActivity.VERTICAL);
		setPositionX((int)(0.2 * screenWidth));
		setPositionY((int)(0.2 * screenHeight));
		setWidth((int)(0.1 * screenWidth));
		setHeight((int)(0.7 * screenHeight));
		timeUpdate = 100;
		currentLong = 0;
	}

	public void show() {
		handler = new Handler();
		if (orientation == BaseActivity.VERTICAL) {
			LayoutParams paramFullProgress = new LayoutParams(width, height);
			paramFullProgress.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			paramFullProgress.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			paramFullProgress.setMargins(positionX, positionY, 0, 0);
			liLayoutMaxProgress.setLayoutParams(paramFullProgress);
			
			android.widget.LinearLayout.LayoutParams param = new android.widget.LinearLayout.LayoutParams (width, currentLong);
			param.setMargins(2, 2, 2, 2);
			textProgress.setLayoutParams(param);
		} else if (orientation == BaseActivity.HORIZONTAL) {
			LayoutParams paramFullProgress = new LayoutParams(width, height);
			paramFullProgress.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			paramFullProgress.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			paramFullProgress.setMargins(positionX, positionY, 0, 0);
			liLayoutMaxProgress.setLayoutParams(paramFullProgress);
			
			android.widget.LinearLayout.LayoutParams param = new android.widget.LinearLayout.LayoutParams (currentLong, height);
			param.setMargins(2, 2, 2, 2);
			textProgress.setLayoutParams(param);
		}
		lengthStep = 0;
		
		handler.postDelayed(runnable, timeUpdate);
	}
	
	public void start() {
		if (orientation == BaseActivity.VERTICAL) {
			lengthStep = (int)(height / (int)(duration/timeUpdate));
			Toast.makeText(this, String.valueOf(100 -(currentLong*100)/height) + "%", Toast.LENGTH_SHORT).show();
		} else if (orientation == BaseActivity.HORIZONTAL){
			lengthStep = (int)(width / (int)(duration/timeUpdate));
			Toast.makeText(this, String.valueOf(100 - (currentLong*100)/width) + "%", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void stop() {
		lengthStep = 0;
		if (orientation == BaseActivity.VERTICAL) {
			Toast.makeText(this, String.valueOf((currentLong*100)/height) + "%", Toast.LENGTH_SHORT).show();
		} else if (orientation == BaseActivity.HORIZONTAL){
			Toast.makeText(this, String.valueOf((currentLong*100)/width) + "%", Toast.LENGTH_SHORT).show();
		}
	}
	
	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getOrientation() {
		return orientation;
	}

	public void setOrientation(int orientation) {
		this.orientation = orientation;
	}

	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnStart:
			start();
			break;
			
		case R.id.btnStop:
			stop();
			break;

		default:
			break;
		}
	}
	
	
	
	Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			currentLong = currentLong + lengthStep;
			Log.d("current Long", String.valueOf(currentLong));
			if (getOrientation() == BaseActivity.VERTICAL){
				android.widget.LinearLayout.LayoutParams param = new android.widget.LinearLayout.LayoutParams (width, currentLong);
				param.setMargins(2, 2, 2, 2);
				textProgress.setLayoutParams(param);
				if(currentLong == height) {
					handler.removeCallbacks(runnable);
					Toast.makeText(EnergyBarActivity.this, "0" + "%", Toast.LENGTH_SHORT).show();
				} else {
					handler.postDelayed(runnable, timeUpdate);
				}
			} else if (getOrientation() == BaseActivity.HORIZONTAL){
				android.widget.LinearLayout.LayoutParams param = new android.widget.LinearLayout.LayoutParams (currentLong, height);
				param.setMargins(2, 2, 2, 2);
				textProgress.setLayoutParams(param);
				if(currentLong == width) {
					handler.removeCallbacks(runnable);
					Toast.makeText(EnergyBarActivity.this, "0" + "%", Toast.LENGTH_SHORT).show();
				} else {
					handler.postDelayed(runnable, timeUpdate);
				}
			}
		}
	};

}
